//
//  FIDControllerTransition.m
//  writeTest
//
//  Created by Fidetro on 2016/12/23.
//  Copyright © 2016年 DTSTON. All rights reserved.
//

#import "FIDControllerTransition.h"

@interface FIDControllerTransition ()

/** 是否dismiss **/
@property(nonatomic,assign) BOOL isDismiss;

@end

static float SpringWithDamping = 0.9f;
static float SpringVelocity = 0.9f;
static float SpringDuration = 0.5f;


@implementation FIDControllerTransition

#pragma mark - --------------------------UIViewControllerTransitioningDelegate--------------------------
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source
{
    self.isDismiss = NO;
    return self;
}


- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed
{
    self.isDismiss = YES;
    return self;
}

#pragma mark - --------------------------UIViewControllerContextTransitioning--------------------------
- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext
{
    return 0.5;
}


- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
    
    UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    
    
    UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    
    
    UIView *containerView = [transitionContext containerView];

    if (self.isDismiss) {
//dismiss的时候
        
        [UIView animateWithDuration:SpringDuration
                              delay:0.0
             usingSpringWithDamping:SpringWithDamping
              initialSpringVelocity:SpringVelocity
                            options:UIViewAnimationOptionCurveEaseInOut
                         animations:^{
                             fromViewController.view.transform = CGAffineTransformMakeScale(0.1, 0.1);
                              fromViewController.view.alpha = 0.0;
                         }
                         completion:^(BOOL finished) {
                             toViewController.view.transform = CGAffineTransformIdentity;
                             fromViewController.view.transform = CGAffineTransformIdentity;
                             [fromViewController.view removeFromSuperview];
                             [transitionContext completeTransition:YES];
                         }];
        
    } else {
//present的时候

        [containerView addSubview:toViewController.view];

        toViewController.view.alpha = 0.0;
        toViewController.view.transform = CGAffineTransformMakeScale(0.7, 0.7);
        
  

        
        
        [UIView animateWithDuration:SpringDuration delay:0.0
             usingSpringWithDamping:SpringWithDamping
              initialSpringVelocity:SpringVelocity
                            options:UIViewAnimationOptionCurveEaseInOut
                         animations:^{
            toViewController.view.alpha = 1.0;
            toViewController.view.transform = CGAffineTransformIdentity;
            fromViewController.view.transform = CGAffineTransformIdentity;
        } completion:^(BOOL finished) {
            [transitionContext completeTransition:YES];
        }];
    }
}
@end
